<template>
  <div>
    <el-form class="input-form"
             label-position="right"
             label-width="120px"
             ref="editForm"
             :rules="propRules"
             :model="form"
    >
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="单号" prop="busKey">
            <span>{{strings.defaultString(form.busKey,'自动设置')}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="申请日期" prop="busDate">
            <span>{{strings.defaultString(form.busDate,'自动设置')}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="汇总人/科室" prop="busUser">
            <span>{{form.busUser}}/{{form.deptName}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" size="medium" type="textarea" :autosize="{ minRows: 1, maxRows: 4}"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-row >
            <el-col :span="12">
              <FortenEsign
                :fileSavePath="'adm_matr_apply'"
                :fileRefTable="'adm_matr_apply'"
                :fileRefId="form.id+'_nursing_manager'"
                :propShowSave="false"
                :propShowSaveUser="true"
                :propAutoSave="false"
                :fileRefIdOnlyOne="'1'"
                ref="auditEsign1"
                :propWidth="200"
                :propHeight="100"
                propTitle="护士长签字"
                :propDisabled="isDisabledUserEsign('auditEsign1')"
                style="    display: inline-block;"
                @after-upload="afterUpload1"
              ></FortenEsign>
            </el-col>
            <el-col :span="12">
              <FortenEsign
                :fileSavePath="'adm_matr_apply'"
                :fileRefTable="'adm_matr_apply'"
                :fileRefId="form.id+'_dept_director'"
                :propShowSave="false"
                :propShowSaveUser="true"
                :propAutoSave="false"
                :fileRefIdOnlyOne="'1'"
                ref="auditEsign2"
                :propWidth="200"
                :propHeight="100"
                propTitle="科主任签字"
                :propDisabled="isDisabledUserEsign('auditEsign2')"
                style="    display: inline-block; margin-left:11px;"
                @after-upload="afterUpload2"
              ></FortenEsign>
            </el-col>
          </el-row>
        </el-col>

        <el-col :span="16" >
          <h4>申请材料(至少上传一个)</h4>
          <FortenUpload
            :title="'申请材料模板'"
            :fileRefTable="'cfg_parameter'"
            :fileSavePath="'cfg_parameter'"
            fileRefId="1339856935157317634"
            :fileType="'all'"
            :listHeight="'25px'"
            :fileMaxNum="999"  :readonly="true"  :isOneLine="true"
            style="max-height: 44px;overflow: auto;"
          ></FortenUpload>
          <FortenUpload
            :fileRefTable="'adm_matr_apply'"
            :fileSavePath="'adm_matr_apply'"
            :fileRefId="form.id"
            :propStatus="propStatus"
            :fileType="'all'"
            :fileMaxNum="999"
            ref="userUpload"
            :fileSize="100"
            :listHeight="'80px'"
            :noTitle="true"  :readonly="strings.defaultString(form.flowStatus,'start') != 'start'"
            style="max-height: 88px;overflow: auto;"
          ></FortenUpload>
        </el-col>
      </el-row>
      <el-collapse v-model="activeNames">

        <!--耗材添加-->
        <el-collapse-item title="申请耗材列表" name="4">
          <data-view
            propViewKey="adm_matr_apply_item"
            propViewForm="adm/matrapplyitem/detail.vue"
            :propInitData="form"
            :propAutoFetchData="false"
            :propHideFlowSelect="true"
            :propReadonly="strings.defaultString(form.flowStatus,'start') != 'start'"
            ref="childDataViewApplyItem"
            :propFlowKey="propParent.propFlowKey"
            class="data-view-inlinebtn"
            @before-button-click="beforeButtonClick"
          >
          </data-view>
        </el-collapse-item>

        <!--流程审批记录-->
       <!-- <el-collapse-item title="流程审批记录" name="5">
        </el-collapse-item>-->
      </el-collapse>
    </el-form>
  </div>
</template>

<script>
/* eslint-disable*/
import FortenUpload from '@/components/common/FortenUpload.vue'
export default {
  data() {
    return {
      form: {},
      viewData: {},
      activeNames: ['1', '3', '4', '5']
    }
  },
  components: {
    FortenUpload
  },
  methods: {
    //刷新申请材料
    fetchDataApplyItem() {
      //固定数据筛选参数
      let wrapper = this.$newWrapper().eq('applyId', this.form.id ? this.form.id : '-1')
      this.$refs.childDataViewApplyItem.setQueryWrapper(wrapper)
    },
    //按钮操作前
    async beforeButtonClick({btn}) {
      console.log('btn,', btn)
      if(btn.code == 'vb-add'){ //添加前如果没保存，则先保存主表
        if (!this.form.id) {
          this.$refs.childDataViewApplyItem.setViewFormVisible(false)
          await this.propParent.ocAsynSaveViewForm()

          if (this.strings.isBlank(this.form.id)) {   //保存失败，排除服务器异常，则一般是校验失败
            if (!this.form.bidStartDate) this.$refs.bidStartDate.focus()
            if (this.form.bidStartDate && !this.form.bidEndDate) this.$refs.bidEndDate.focus()
          } else {
            this.$refs.childDataViewApplyItem.viewAdd()
          }
        }

      }
    },
    async getForm() {
      return this.form
    },
    //是否显示电子签名
    isDisabledUserEsign(key) {
      let { roleCodes = [], code } = this.utils.getCurrentUser()
      //护士长签字 ,当前流程是护士长审核，并且登录人是拥有护士长角色
      if (key ==  'auditEsign1' && this.form.flowStatus == 'nursingManager' ) {
        if (code == 'admin' ) return false
        if (roleCodes.includes('adm_nursing_manager')) {
          return false
        }
      }

      //科室主任签字 ,当前流程是科室主任审核，并且登录人是拥有科室主任角色
      if (key ==  'auditEsign2' && this.form.flowStatus == 'submitNursingManager' ) {
        if (code == 'admin' ) return false
        if (roleCodes.includes('adm_dept_director')) {
          return false
        }
      }

      return true
    },
    afterUpload1(param){
      this.$set(this.form, 'signPath1', param.filePath)
      this.propParent.ocAsynSaveViewForm()
    },
    afterUpload2(param){
      this.$set(this.form, 'signPath2', param.filePath)
      this.propParent.ocAsynSaveViewForm()
    }
  },
  created() {
    let that = this
    this.$emit('event-register', {
      validate: (callback) => {
        this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        if (!this.form.id) {
          let currentUser = this.utils.getCurrentUser()
          this.$set(this.form, 'busUser', currentUser.name)
          this.$set(this.form, 'deptId', currentUser.deptId)
          this.$set(this.form, 'deptName', currentUser.deptName)
          this.$set(this.form, 'busDate', new Date().format(this.dates.defaultDateTimePattern))
        } else {
          this.fetchDataApplyItem()
        }

        //setSignPath
        if (this.form.signPath1) {
          this.$refs.auditEsign1.setSignPath(this.form.signPath1)
        } else {
          if (!this.isDisabledUserEsign('auditEsign1')) this.$refs.auditEsign1.setUserSign()
        }
        if (this.form.signPath2) {
          this.$refs.auditEsign2.setSignPath(this.form.signPath2)
        } else {
          if (!this.isDisabledUserEsign('auditEsign2')) this.$refs.auditEsign2.setUserSign()
        }
      },
      afterSave: () => {
        that.propParent.fetchData()
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow:  (flowButtonKey) => {
        //至少一个附件
        if (that.strings.defaultString(that.form.flowStatus,'start') == 'start' && that.$refs.userUpload.fileList < 1) {
          that.$message.warning('请至少上传一个申请材料后，再提交')
          return false
        }


        //提交前，进行一些校验
        let base641 = that.$refs.auditEsign1.getData()
        let base642 = that.$refs.auditEsign2.getData()

        if (!that.isDisabledUserEsign('auditEsign1') && that.strings.isBlank(that.form.signPath1)) {
          if (!base641) {
            that.$message.warning('当前为护士长审核阶段，请设置护士长签名')
            return false
          }
          if (that.strings.isBlank(that.$refs.auditEsign1.filePath)) {
            that.$message.warning('您已经设置完签名，请点击签名区域保存按钮')
            return false
          }
          that.$set(that.form,'signPath1',that.$refs.auditEsign1.filePath )
          // that.propParent.ocAsynSaveViewForm()
        }


        if (!that.isDisabledUserEsign('auditEsign2')  && that.strings.isBlank(that.form.signPath2)) {
          if ( !base642) {
            that.$message.warning('当前为科室主任审核阶段，请设置科室主任签名')
            return false
          }
          if (that.strings.isBlank(that.$refs.auditEsign2.filePath)) {
            that.$message.warning('您已经设置完签名，请点击签名区域保存按钮')
            return false
          }
          that.$set(that.form,'signPath2',that.$refs.auditEsign2.filePath )
          // that.propParent.ocAsynSaveViewForm()
        }

        return true
      },
      afterFlow: (flowButtonKey) => {
        if (flowButtonKey == 'start') this.propReadonly = false
      },
      beforeClose: () => {
        return true
      },
      afterClick(button, data, viewForm) {
        let {key: buttonKey} = button
        if (buttonKey == 'vfb-copy') { //复制
          that.$confirm('是否复制，包含主单据及请领明细?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'success'
          }).then(() => {
            that.utilVue.loadShow(true)
            that.$ajax.get(`/adm/matr_apply/copy/${data.id}`).then(resp => {
              that.utilVue.loadHide()
              if (resp.code == 0) {
                that.propParent.ocRefresh()
                that.$confirm('申请单复制成功，是否打开进行编辑?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'success'
                }).then(() => {
                  that.propParent.viewRow(resp.data.id)
                }).catch(() => {
                })
              }
            })
          }).catch(() => {
          })
        }
      }
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object
  }
}
</script>

<style lang="scss" scoped>
  .container {
    width: 100%;
    height: 100%;
  }
</style>
